<template>
    <div class="login">
    </div>
</template>
<script>
    export default {
        name: "Login",
        data() {
            return {
                url: {
                    grant: this.axiosUrl + 'Login/grant',
                    upper: this.axiosUrl + 'Login/upper',
                },
                code: ''
            }
        },
        components: {},
        mounted() {
            if (this.$route.query.tokens) {
                localStorage.setItem('state', this.$route.query.tokens)
            }
            if (this.$route.query.market){
                localStorage.setItem('market', this.$route.query.market)
            }
            if (localStorage.getItem('token')) {
                this.$router.push({path:'/'})
            } else {
                if (this.$route.query.code) {
                    this.upper()
                } else {
                    this.grant()
                }
            }
        },
        methods: {
            grant() { // 获取微信链接
                let _this = this
                this.$http.post(_this.url.grant, {}, (res) => {
                    location.href = res
                })
            },
            upper() {
                let _this = this
                this.$http.post(_this.url.upper, {
                    state: localStorage.getItem('state') ? localStorage.getItem('state') : undefined,
                    code: _this.$route.query.code
                }, (res) => {
                    console.log(localStorage.getItem('state'))
                    localStorage.setItem('token', res)
                    localStorage.removeItem('state')
                    _this.$router.push({path: '/'})
                })
            }
        }
    }
</script>
<style lang="stylus" scoped>
</style>
